<?php

namespace Apipad\Controller;

use Think\Controller;

//数据库表库存
header("content-type:application/json;charset=utf-8");

class MysqlsController extends CommonController
{

    //标准数据库默认分类
    public function mysqls_cate()
    {
        $model = D('UserMysqlDefaultCate');
        $map = [];
        $map['pid'] = 0;
        $map['stype'] = 1;
        $map['isdel'] = 0;
        $list = $model->where($map)->order('add_time desc')->select();
        $data['code'] = 200;
        $data['msg'] = '查询成功';
        $data['data']['list'] = $list;
        exit(json_encode($data));
    }

    public function mct_list()
    {
        $detail = I('post.');
        $user_technology = D("UserMysqlList");
        $page = $detail['page'] ? $detail['page'] : 1;
        $limit = $detail['limit'] ? $detail['limit'] : 15;
        $sex = $detail['sex'];
        if ($sex == '') {
            $data_cc['code'] = 0;
            $data_cc['msg'] = '性别不能为空';
            exit(json_encode($data_cc));
        }
        $kewords = $detail['kewords'];
        $cate_id = $detail['cate_id'] ?: 2;
        $uid = UID;
        $where['isdel'] = 0;
        $where['uid'] = $uid;
        $where['sex'] = $sex;
        if ($kewords) {
            $where['high|size|weight'] = ['like', '%' . $kewords . '%'];
        }
        if ($cate_id) {
            //根据cate_id查询 来使用哪张表
            $user_mysql_cate = D("UserMysqlDefaultCate");
            $sht_c = $user_mysql_cate->where(['id' => $cate_id])->getField('sht_c');
            if ($sht_c == 1) {
                $user_technology = M("user_mysql_listc");
            }
            $where['cat_id'] = $cate_id;
        }
        $count = $user_technology->where($where)->count();
        $list = $user_technology
            ->where($where)
            ->order('id desc')->page($page, $limit)->select();
        $data_cc['code'] = 200;
        $data_cc['msg'] = '查询成功';
        $data_cc['data']['count'] = $count;
        $data_cc['data']['list'] = $list;
        exit(json_encode($data_cc));
    }

    //上装添加
    public function upper_add()
    {
        $detail = I('post.');
        $model = M('user_mysql_list');
        if (empty($detail['cate_id'])) {
            $data_cc['code'] = 0;
            $data_cc['msg'] = '分类ID不能为空';
            exit(json_encode($data_cc));
        }
        // if(empty($detail['sex'])){
        if ($detail['sex'] == '') {
            $data_cc['code'] = 0;
            $data_cc['msg'] = '性别不能为空';
            exit(json_encode($data_cc));
        }
        if (empty($detail['high'])) {
            $data_cc['code'] = 0;
            $data_cc['msg'] = '请填写身高';
            exit(json_encode($data_cc));
        }
        if (empty($detail['weight'])) {
            $data_cc['code'] = 0;
            $data_cc['msg'] = '请填写体重';
            exit(json_encode($data_cc));
        }
        if (empty($detail['size'])) {
            $data_cc['code'] = 0;
            $data_cc['msg'] = '请填写尺码';
            exit(json_encode($data_cc));
        }
        $weight = $detail['weight'];
        $jo_res = $weight % 2;
        if ($jo_res != 0) {
            $data_cc['code'] = 0;
            $data_cc['msg'] = '体重请填写偶数';
            exit(json_encode($data_cc));
        }
        $where['uid'] = UID;
        $where['cat_id'] = $detail['cate_id'];
        $where['sex'] = $detail['sex'];
        $where['high'] = $detail['high'];
        $where['size'] = $detail['size'];
        $where['weight'] = $detail['weight'];
        $is_data = $model->where($where)->find();
        if ($is_data) {
            $data_cc['code'] = 0;
            $data_cc['msg'] = '当前身高,体重,尺码信息已存在,无法添加';
            exit(json_encode($data_cc));
        }
        $s_data['uid'] = UID;
        $s_data['cat_id'] = $detail['cate_id'];
        $s_data['sex'] = $detail['sex'];
        $s_data['high'] = $detail['high'];
        $s_data['size'] = $detail['size'];
        $s_data['weight'] = $detail['weight'];
        $s_data['qiancyi'] = $detail['qiancyi'];
        $s_data['hzhc'] = $detail['hzhc'];
        $s_data['cyxw'] = $detail['cyxw'];
        $s_data['chenyiyaow'] = $detail['chenyiyaow'];
        $s_data['zhonyao'] = $detail['zhonyao'];
        $s_data['jiankuan'] = $detail['jiankuan'];
        $s_data['xiuchang'] = $detail['xiuchang'];
        $s_data['linwei'] = $detail['linwei'] ?: "";
        $s_data['feixiu'] = $detail['feixiu'] ?: "";
        $s_data['xiabai'] = $detail['xiabai'] ?: "";
        $s_data['add_time'] = time();
        $result = $model->add($s_data);
        if ($result) {
            $data_cc['code'] = 200;
            $data_cc['msg'] = '添加成功';
            exit(json_encode($data_cc));
        } else {
            $data_cc['code'] = 0;
            $data_cc['msg'] = '添加失败';
            exit(json_encode($data_cc));
        }
    }

    //上装编辑
    public function upper_edit()
    {
        $detail = I('post.');
        $model = M('user_mysql_list');
        $id = $detail['id'];
        if (empty($id)) {
            $data_cc['code'] = 0;
            $data_cc['msg'] = 'Id不能为空';
            exit(json_encode($data_cc));
        }
        if (empty($detail['high'])) {
            $data_cc['code'] = 0;
            $data_cc['msg'] = '请填写身高';
            exit(json_encode($data_cc));
        }
        if (empty($detail['weight'])) {
            $data_cc['code'] = 0;
            $data_cc['msg'] = '请填写体重';
            exit(json_encode($data_cc));
        }
        if (empty($detail['size'])) {
            $data_cc['code'] = 0;
            $data_cc['msg'] = '请填写尺码';
            exit(json_encode($data_cc));
        }
        $weight = $detail['weight'];
        $jo_res = $weight % 2;
        if ($jo_res != 0) {
            $data_cc['code'] = 0;
            $data_cc['msg'] = '体重请填写偶数';
            exit(json_encode($data_cc));
        }
        $s_data['high'] = $detail['high'];
        $s_data['size'] = $detail['size'];
        $s_data['weight'] = $detail['weight'];
        $s_data['qiancyi'] = $detail['qiancyi'];
        $s_data['hzhc'] = $detail['hzhc'];
        $s_data['cyxw'] = $detail['cyxw'];
        $s_data['chenyiyaow'] = $detail['chenyiyaow'];
        $s_data['zhonyao'] = $detail['zhonyao'];
        $s_data['jiankuan'] = $detail['jiankuan'];
        $s_data['xiuchang'] = $detail['xiuchang'];
        $s_data['linwei'] = $detail['linwei'] ?: "";
        $s_data['feixiu'] = $detail['feixiu'] ?: "";
        $s_data['xiabai'] = $detail['xiabai'] ?: "";
        $result = $model->where(['id' => $id])->save($s_data);
        if ($result) {
            $data_cc['code'] = 200;
            $data_cc['msg'] = '编辑成功';
            exit(json_encode($data_cc));
        } else {
            $data_cc['code'] = 0;
            $data_cc['msg'] = '编辑失败';
            exit(json_encode($data_cc));
        }
    }

    //上装详情
    public function upper_detail()
    {
        $detail = I('post.');
        $id = $detail['id'];
        if (empty($id)) {
            $data_cc['code'] = 0;
            $data_cc['msg'] = 'ID不能为空';
            exit(json_encode($data_cc));
        }
        $model = D("UserMysqlList");
        $info = $model->where(['id' => $id, 'isdel' => 0])->find();
        if ($info) {
            $data_cc['code'] = 200;
            $data_cc['msg'] = '查询成功';
            $data_cc['data'] = $info;
            exit(json_encode($data_cc));
        } else {
            $data_cc['code'] = 0;
            $data_cc['msg'] = '信息不存在';
            exit(json_encode($data_cc));
        }
    }

    //上装删除
    public function upper_del()
    {
        $detail = I('');
        $id = $detail['id'];
        if (empty($id)) {
            $data['code'] = 0;
            $data['msg'] = 'ID不能为空';
            exit(json_encode($data));
        }
        $model = M('user_mysql_list');
        $d_model = M('user_private_customer');
        $info = $model->where(['id' => $id])->find();
        $d_info = $d_model->where(['uid' => UID, 'upper_id' => $id])->find();
        if ($d_info) {
            $data['code'] = 0;
            $data['msg'] = '该数据存在关联数据,不能删除';
            exit(json_encode($data));
        }
        $map['id'] = $id;
        $s_data['isdel'] = 1;
        $s_data['del_time'] = time();
        $result = $model->where($map)->save($s_data);
        if ($result) {
            $data['code'] = 200;
            $data['msg'] = '删除成功';
            exit(json_encode($data));
        } else {
            $data['code'] = 0;
            $data['msg'] = '删除失败';
            exit(json_encode($data));
        }
    }

    //下装添加
    public function below_add()
    {
        $detail = I('post.');
        $model = M('user_mysql_listc');
        if (empty($detail['cate_id'])) {
            $data_cc['code'] = 0;
            $data_cc['msg'] = '分类ID不能为空';
            exit(json_encode($data_cc));
        }
        if ($detail['sex'] == '') {
            $data_cc['code'] = 0;
            $data_cc['msg'] = '性别不能为空';
            exit(json_encode($data_cc));
        }
        if (empty($detail['high'])) {
            $data_cc['code'] = 0;
            $data_cc['msg'] = '请填写身高';
            exit(json_encode($data_cc));
        }
        if (empty($detail['weight'])) {
            $data_cc['code'] = 0;
            $data_cc['msg'] = '请填写体重';
            exit(json_encode($data_cc));
        }
        if (empty($detail['size'])) {
            $data_cc['code'] = 0;
            $data_cc['msg'] = '请填写尺码';
            exit(json_encode($data_cc));
        }
        $weight = $detail['weight'];
        $jo_res = $weight % 2;
        if ($jo_res != 0) {
            $data_cc['code'] = 0;
            $data_cc['msg'] = '体重请填写偶数';
            exit(json_encode($data_cc));
        }
        $where['uid'] = UID;
        $where['cat_id'] = $detail['cate_id'];
        $where['sex'] = $detail['sex'];
        $where['high'] = $detail['high'];
        $where['size'] = $detail['size'];
        $where['weight'] = $detail['weight'];
        $is_data = $model->where($where)->find();
        if ($is_data) {
            $data_cc['code'] = 0;
            $data_cc['msg'] = '当前身高,体重,尺码信息已存在,无法添加';
            exit(json_encode($data_cc));
        }
        $s_data['uid'] = UID;
        $s_data['cat_id'] = $detail['cate_id'];
        $s_data['sex'] = $detail['sex'];
        $s_data['high'] = $detail['high'];
        $s_data['size'] = $detail['size'];
        $s_data['weight'] = $detail['weight'];
        $s_data['kuchang'] = $detail['kuchang'];
        $s_data['cpyaowei'] = $detail['cpyaowei'];
        $s_data['cptunwei'] = $detail['cptunwei'];
        $s_data['qzhidan'] = $detail['qzhidan'];
        $s_data['chenyiyaow'] = $detail['chenyiyaow'];
        $s_data['jiaokou'] = $detail['jiaokou'];
        $s_data['qianlang'] = $detail['qianlang'];
        $s_data['houlang'] = $detail['houlang'];
        $s_data['tuiwei'] = $detail['tuiwei'];
        $s_data['menjinc'] = $detail['menjinc'];
        $s_data['zhotui'] = $detail['zhotui'];
        $s_data['add_time'] = time();
        $result = $model->add($s_data);
        if ($result) {
            $data_cc['code'] = 200;
            $data_cc['msg'] = '添加成功';
            exit(json_encode($data_cc));
        } else {
            $data_cc['code'] = 0;
            $data_cc['msg'] = '添加失败';
            exit(json_encode($data_cc));
        }


    }

    //下装编辑
    public function below_edit()
    {
        $detail = I('post.');
        $model = M('user_mysql_listc');
        $id = $detail['id'];
        if (empty($id)) {
            $data_cc['code'] = 0;
            $data_cc['msg'] = 'Id不能为空';
            exit(json_encode($data_cc));
        }
        if (empty($detail['high'])) {
            $data_cc['code'] = 0;
            $data_cc['msg'] = '请填写身高';
            exit(json_encode($data_cc));
        }
        if (empty($detail['weight'])) {
            $data_cc['code'] = 0;
            $data_cc['msg'] = '请填写体重';
            exit(json_encode($data_cc));
        }
        if (empty($detail['size'])) {
            $data_cc['code'] = 0;
            $data_cc['msg'] = '请填写尺码';
            exit(json_encode($data_cc));
        }
        $weight = $detail['weight'];
        $jo_res = $weight % 2;
        if ($jo_res != 0) {
            $data_cc['code'] = 0;
            $data_cc['msg'] = '体重请填写偶数';
            exit(json_encode($data_cc));
        }
        $s_data['high'] = $detail['high'];
        $s_data['size'] = $detail['size'];
        $s_data['weight'] = $detail['weight'];
        $s_data['kuchang'] = $detail['kuchang'];
        $s_data['cpyaowei'] = $detail['cpyaowei'];
        $s_data['cptunwei'] = $detail['cptunwei'];
        $s_data['qzhidan'] = $detail['qzhidan'];
        $s_data['chenyiyaow'] = $detail['chenyiyaow'];
        $s_data['jiaokou'] = $detail['jiaokou'];
        $s_data['qianlang'] = $detail['qianlang'];
        $s_data['houlang'] = $detail['houlang'];
        $s_data['tuiwei'] = $detail['tuiwei'];
        $s_data['menjinc'] = $detail['menjinc'];
        $s_data['zhotui'] = $detail['zhotui'];
        $result = $model->where(['id' => $id])->save($s_data);
        if ($result) {
            $data_cc['code'] = 200;
            $data_cc['msg'] = '编辑成功';
            exit(json_encode($data_cc));
        } else {
            $data_cc['code'] = 0;
            $data_cc['msg'] = '编辑失败';
            exit(json_encode($data_cc));
        }
    }

    //下装详情
    public function below_detail()
    {
        $detail = I('post.');
        $id = $detail['id'];
        if (empty($id)) {
            $data_cc['code'] = 0;
            $data_cc['msg'] = 'ID不能为空';
            exit(json_encode($data_cc));
        }
        $model = M('user_mysql_listc');
        $info = $model->where(['id' => $id, 'isdel' => 0])->find();
        if ($info) {
            $data_cc['code'] = 200;
            $data_cc['msg'] = '查询成功';
            $data_cc['data'] = $info;
            exit(json_encode($data_cc));
        } else {
            $data_cc['code'] = 0;
            $data_cc['msg'] = '信息不存在';
            exit(json_encode($data_cc));
        }
    }

    //下装删除
    public function below_del()
    {
        $detail = I('');
        $id = $detail['id'];
        if (empty($id)) {
            $data['code'] = 0;
            $data['msg'] = 'ID不能为空';
            exit(json_encode($data));
        }
        $model = M('user_mysql_listc');
        $d_model = M('user_private_customer');
        $info = $model->where(['id' => $id])->find();
        $d_info = $d_model->where(['uid' => UID, 'below_id' => $id])->find();
        if ($d_info) {
            $data['code'] = 0;
            $data['msg'] = '该数据存在关联数据,不能删除';
            exit(json_encode($data));
        }
        $map['id'] = $id;
        $s_data['isdel'] = 1;
        $s_data['del_time'] = time();
        $result = $model->where($map)->save($s_data);
        if ($result) {
            $data['code'] = 200;
            $data['msg'] = '删除成功';
            exit(json_encode($data));
        } else {
            $data['code'] = 0;
            $data['msg'] = '删除失败';
            exit(json_encode($data));
        }
    }

    //模板下载
    function export()
    {
        $type = I('type');
        $str = '';
        if ($type == 0) {
            //上装
            $str = '上装模板表';
            $data['field'][] = '身高';
            $data['field'][] = '体重(斤)';
            $data['field'][] = '尺码';
            $data['field'][] = '领围';
            $data['field'][] = '前衣长';
            $data['field'][] = '后中长';
            $data['field'][] = '成衣胸围';
            $data['field'][] = '中腰';
            $data['field'][] = '成衣腰围';
            $data['field'][] = '肩宽';
            $data['field'][] = '袖长';
            $data['field'][] = '袖肥';
            $data['field'][] = '下摆';

        } elseif ($type == 1) {
            //下装
            $str = '下装模板表';
            $data['field'][] = '身高';
            $data['field'][] = '体重(斤)';
            $data['field'][] = '尺码';
            $data['field'][] = '裤长';
            $data['field'][] = '成品腰围';
            $data['field'][] = '成品臀围';
            $data['field'][] = '前直档';
            $data['field'][] = '脚口';
            $data['field'][] = '前浪';
            $data['field'][] = '后浪';
            $data['field'][] = '腿围';
            $data['field'][] = '中腿';
            $data['field'][] = '门襟长';
        } else {
            $data['code'] = 0;
            $data['msg'] = '参数错误';
            exit(json_encode($data));
        }
        excelDown($data, $str);
    }

    //导入Excel
    function import()
    {
        //$url = 'https://wages-zymaimai.oss-cn-beijing.aliyuncs.com/2023/04/15/643a6ad9f0698.xlsx';
        $excel_path = I('excel_path');
        $cat_id = I('cat_id');
        $sex = I('sex');
        $type = I('type');
        if (!$excel_path) {
            $data_cc['code'] = 0;
            $data_cc['msg'] = 'excel地址必须！';
            exit(json_encode($data_cc));
        }
        if (I('type') == '') {
            $data_cc['code'] = 0;
            $data_cc['msg'] = '模板类型为空';
            exit(json_encode($data_cc));
        }
        if (I('sex') == '') {
            $data_cc['code'] = 0;
            $data_cc['msg'] = '请选择性别';
            exit(json_encode($data_cc));
        }
        if (!I('cat_id')) {
            $data_cc['code'] = 0;
            $data_cc['msg'] = '请选择类型';
            exit(json_encode($data_cc));
        }
        if ($type == 0) {
            //上装
            $model = M('user_mysql_list');
        } elseif ($type == 1) {
            //下装
            $model = M('user_mysql_listc');
        } else {
            $data_cc['code'] = 0;
            $data_cc['msg'] = '参数错误';
            exit(json_encode($data_cc));
        }

        $path = UID . '_attn.xlsx';//考勤excel
        file_put_contents($path, file_get_contents($excel_path));
        $exc_data = importExcel($path);
        $model->startTrans();
        foreach ($exc_data as $key => $val) {
            $data = [];
            if ($key == 0) {
                continue;
            }
            if (empty($val[2])) {
                $data_cc['code'] = 0;
                $data_cc['msg'] = '尺寸不能为空或者导入模板错误';
                exit(json_encode($data_cc));
            }
            if (empty($val[0])) {
                $data_cc['code'] = 0;
                $data_cc['msg'] = '身高不能为空或者导入模板错误';
                exit(json_encode($data_cc));
            }
            if ($type == 0) {
                $data['cat_id'] = $cat_id;
                $data['sex'] = $sex;
                $data['uid'] = UID;
                $data['high'] = $val[0];
                $data['weight'] = $val[1];
                $data['size'] = $val[2];
                $data['linwei'] = $val[3];
                $data['qiancyi'] = $val[4];
                $data['hzhc'] = $val[5];
                $data['cyxw'] = $val[6];
                $data['zhonyao'] = $val[7];
                $data['chenyiyaow'] = $val[8];
                $data['jiankuan'] = $val[9];
                $data['xiuchang'] = $val[10];
                $data['feixiu'] = $val[11];
                $data['xiabai'] = $val[12];
            } else {
                $data['cat_id'] = $cat_id;
                $data['sex'] = $sex;
                $data['uid'] = UID;
                $data['high'] = $val[0];
                $data['weight'] = $val[1];
                $data['size'] = $val[2];
                $data['kuchang'] = $val[3];
                $data['cpyaowei'] = $val[4];
                $data['cptunwei'] = $val[5];
                $data['qzhidan'] = $val[6];
                $data['jiaokou'] = $val[7];
                $data['qianlang'] = $val[8];
                $data['houlang'] = $val[9];
                $data['tuiwei'] = $val[10];
                $data['zhotui'] = $val[11];
                $data['menjinc'] = $val[12];
            }
            $data['add_time'] = time();
            $s_wdata['cat_id'] = $cat_id;
            $s_wdata['sex'] = $sex;
            $s_wdata['uid'] = UID;
            $s_wdata['high'] = $val[0];
            $s_wdata['weight'] = $val[1];
            $s_wdata['size'] = $val[2];
            $s_wdata['isdel'] = 0;
            $s_vo = $model->where($s_wdata)->find();
            if ($s_vo) {
                continue;
            } else {
                $res = $model->add($data);
                if ($res === false) {
                    $data_cc['code'] = 0;
                    $data_cc['msg'] = '添加数据失败';
                    exit(json_encode($data_cc));
                }
            }
        }
        $model->commit();
        unlink($path);
        $data_cc['code'] = 200;
        $data_cc['msg'] = '导入成功';
        exit(json_encode($data_cc));
    }
}